package com.bulletelc.apollopre.module.main.center.orders.detail;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.entity.OrderDetailBean;
import com.bulletelc.apollopre.manager.imageload.ImageLoaderManager;
import com.bulletelc.apollopre.module.main.center.orders.refunk.RefunkActivity;
import com.bulletelc.apollopre.utils.MyUtil;

import java.math.BigDecimal;
import java.util.List;

/**
 * author: lhl
 * date  : 2018/01/25
 * desc  : 订单详情中的商品列表Adapter
 */

public class OrderListAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private List<OrderDetailBean.SalesOrderItemsBean> salesOrderItemsBeans;
    private int orderState;
    private LayoutInflater inflater;
    private String orderId;//大订单ID

    public OrderListAdapter(Context context, List<OrderDetailBean.SalesOrderItemsBean> salesOrderItemsBeans, int orderState) {
        this.context = context;
        this.salesOrderItemsBeans = salesOrderItemsBeans;
        this.orderState = orderState;
        this.inflater = LayoutInflater.from(context);
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public void setOrderState(int orderState) {
        this.orderState = orderState;
    }

    @Override
    public int getCount() {
        return salesOrderItemsBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return salesOrderItemsBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        OrderDetailBean.SalesOrderItemsBean bean = salesOrderItemsBeans.get(position);
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.order_detail_list_item, parent, false);
            holder.iv = convertView.findViewById(R.id.ivProductImg);
            holder.tvName = convertView.findViewById(R.id.tvProductName);
            holder.tvSku = convertView.findViewById(R.id.tvSku);
            holder.tvReturnOrder = convertView.findViewById(R.id.tvReturnOrder);
            holder.tvPrice = convertView.findViewById(R.id.tvProductPrice);
            holder.tvNum = convertView.findViewById(R.id.tvProductNumShow);
            holder.tvSubPrice = convertView.findViewById(R.id.tvSubPrice);

            holder.tvReturnOrder.setOnClickListener(this);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvName.setText(bean.getProduct_name());
        holder.tvSku.setText(bean.getSku_label());

        holder.tvPrice.setText("¥" + MyUtil.format2(bean.getSale_price()));
        holder.tvNum.setText("x" + bean.getQuantity());
        holder.tvSubPrice.setText("¥" + getSubTotalPrice(bean.getSale_price(), bean.getQuantity()));

        if (bean.getQuantity() - bean.getRefund_order_count() == 0) {
            int itemState = bean.getState();
            holder.tvReturnOrder.setVisibility(View.VISIBLE);
            switch (itemState) {
                case GlobalConfig.ORDER_ITEM_STATE_APPLY:
                    holder.tvReturnOrder.setText("申请售后");
                    holder.tvReturnOrder.setEnabled(true);
                    break;
                case GlobalConfig.ORDER_ITEM_STATE_FINISH:
                    holder.tvReturnOrder.setText("售后完成");
                    holder.tvReturnOrder.setEnabled(false);
                    break;
                case GlobalConfig.ORDER_ITEM_STATE_APPLYING:
                    holder.tvReturnOrder.setText("售后中");
                    holder.tvReturnOrder.setEnabled(false);
                    break;
                default:

                    break;
            }
        } else if (bean.getQuantity() - bean.getRefund_order_count() > 0){
            holder.tvReturnOrder.setText("申请售后");
            holder.tvReturnOrder.setEnabled(true);
        }

        ImageLoaderManager.getInstance().loadImage(ImageLoaderManager.getDefaultOptions(holder.iv, bean.getProduct_images().getOriginal()));

        switch (orderState) {
            case GlobalConfig.ORDER_STATE_UN_SEND:
            case GlobalConfig.ORDER_STATE_UN_SIGN:
            case GlobalConfig.ORDER_STATE_COMPLETE:
                if (bean.getQuantity() - bean.getRefund_order_count() > 0) {
                    holder.tvReturnOrder.setText("申请售后");
                    holder.tvReturnOrder.setEnabled(true);
                    holder.tvReturnOrder.setVisibility(View.VISIBLE);
                    holder.tvReturnOrder.setTag(position);
                }
                break;

        }

        return convertView;
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        switch (v.getId()) {
            case R.id.tvReturnOrder: //跳转到 提交退货界面
                OrderDetailBean.SalesOrderItemsBean salesOrder = salesOrderItemsBeans.get(position);
                Intent intent = new Intent(context, RefunkActivity.class);
                intent.putExtra(IntentCode.EXTRA_KEY_ORDER_ID, orderId); //订单ID
                intent.putExtra(IntentCode.EXTRA_KEY_ORDER_ITEM_ID, salesOrder.getOrder_item_id()); //子订单ID
                intent.putExtra(IntentCode.EXTRA_KEY_ORDER_QUANTITY, salesOrder.getQuantity() - salesOrder.getRefund_order_count()); //可退数量
                context.startActivity(intent);
                ((AppCompatActivity) context).finish();
                break;
        }
    }

    /**
     * 进行精度除法
     *
     * @param salePrice
     * @param quantity
     * @return
     */
    private String divide(double salePrice, int quantity) {
        BigDecimal price = new BigDecimal(salePrice);
        BigDecimal bigDecimal = price.divide(new BigDecimal(quantity), 2, BigDecimal.ROUND_HALF_UP);
        return MyUtil.format2(Double.parseDouble(bigDecimal.toString()));
    }

    private String getSubTotalPrice(double salePrice, int quantity) {
        double total = salePrice * quantity;
        return MyUtil.format2(total);
    }

    class ViewHolder {
        ImageView iv;
        TextView tvName;
        TextView tvSku;
        TextView tvReturnOrder;
        TextView tvPrice;
        TextView tvNum;
        TextView tvSubPrice;
    }
}
